package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constrant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author FCJ
 * @date 2020-01-02 11:37
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;
    @Reference
    private CheckItemService checkItemService;

    //添加检查组
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.add(checkGroup, checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    //根据条件查询分页
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
        return pageResult;
    }

    //编辑检查组的回显信息
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            CheckGroup checkGroup = checkGroupService.findOne(id);//根据id查找包含所含检查项的检查组
            List<CheckItem> checkItems = checkItemService.findAll();//查询所有检查项
            List<Integer> checkItemIds = new ArrayList<>();//获取检查组所含检查项的id
            for (CheckItem checkItem : checkGroup.getCheckItems()) {
                checkItemIds.add(checkItem.getId());
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("checkGroup", checkGroup);
            map.put("checkItemIds", checkItemIds);
            map.put("checkItems", checkItems);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, map);//执行成功，返回数据
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);//执行失败，返回错误信息
        }
    }

    //添加检查组
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.edit(checkGroup, checkitemIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

    }

    //根据条件查询分页
    @RequestMapping("/deleteCheckGroupById")
    public Result deleteCheckGroupById(Integer id) {
        try {
            checkGroupService.deleteRelation(id);
            checkGroupService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
    //查询所有检查组
    @RequestMapping("/findCheckGroup")
    public Result findCheckGroup(){
        try {
            List<CheckGroup> allCheckGroup = checkGroupService.findAllCheckGroup();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,allCheckGroup);
        }catch (Exception e){
            return  new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}